<?php


namespace Application\Model;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;


class ClientTable extends AbstractTableGateway {
	
	protected $table ='client';
	protected $tablename ='client';
	
	public function __construct(Adapter $adapter) {
		$this->adapter = $adapter;
		$this->resultSetPrototype = new ResultSet(new Client);
		$this->initialize();
	}
	
	//retourne tout le contenu de la table
	public function fetchAll()
	{
		$resultSet = $this->select();
		return $resultSet;
	}
	
	//retourne un client dont l'id est passé en paramètre
	public function getClient($id)
	{
		$id = (int) $id;
		$rowset = $this->select(array('id_client' => $id));
		$row = $rowset->current();
		if (!$row) {
			throw new \Exception("Could not find row $id");
		}
		return $row;
	}
	
	public function getClientLogin($login)
	{
		//$username = (int) $username;
		$rowset = $this->select(array('login' => $login));
		$row = $rowset->current();
		if (!$row) {
			//throw new \Exception("Could not find $username ");
			return null;
		}
		return $row;
	}
	
	public function saveClient(Client $client)
	{
		$data = array(
	
				'nom' => $client->nom,
				'prenom' => $client->prenom,
				'fonction' => $fonction->fonction,
				'entreprise' => $client->entreprise,
				'email' => $client->email,
				'telephone' => $client->telephone,
				'login' => $client->login,
				'password' => $client->password,
				'objet' => $client->objet,
				'date_inscription' => $client->date_inscrip,
				
		);
		$id = (int)$client->id_client;
		if ($id == 0) {
			$this->insert($data);
		} else {
			if ($this->getClient($id)) {
				$this->update($data, array('id_client' => $id));
			} else {
				throw new \Exception('Form id does not exist');
			}
		}
	}
	
	public function addClient($nom, $prenom,$fonction,$entreprise,$email,$telephone,$login,$password,$objet,$date_inscrip)
	{
		$data = array(
				'nom' => $nom,
				'prenom' => $prenom,
				'fonction' => $fonction,
				'entreprise' => $entreprise,
				'email' => $email,
				'telephone' => $telephone,
				'login' => $login,
				'password' => $password,
				'objet' => $objet,
				'date_inscription' =>$date_inscrip,
				
			//	$validator = new \Zend\Validator\File\Md5('3b3652f336522365223');
		);
		$this->insert($data);
	}
	
	public function updateClient($nom, $prenom,$entreprise,$email,$telephone,$login,$password,$objet,$date_inscrip)
	{
		$data = array(
				'nom' => $username,
				'prenom' => $prenom,
				'fonction' => $fonction,
				'entreprise' => $entreprise,
				'email' => $email,
				'telephone' => $telephone,
				'login' => $login,
				'password' => $password,
				'objet' => $objet,
				'date_inscription' =>$date_inscrip,
		);
		$this->update($data, array('id_client' => $id));
	}
	public function deleteClient($id)
	{
		$this->delete(array('id_client' => $id));
	}
}

?>